﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Condimar.Entities;
using Condimar.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Condimar.UI
{
    public partial class FrmBuscarVenta : Form
    {
        [Dependency]
        public IVentaBL ventaBL { get; set; }

        [Dependency]
        public IDetalleVentaBL detVentaBL { get; set; }

        [Dependency]
        public IClienteJuridicoBL cliJuriBL { get; set; }

        [Dependency]
        public IClienteNaturalBL cliNatBL { get; set; }

        public FrmBuscarVenta()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmBuscarVenta_Load(object sender, EventArgs e)
        {
            cargaData();
        }

        public void cargaData()
        {
            ventaBindingSource.DataSource = ventaBL.GetAllFromVenta();
        }

        private void ventaDataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            var venta = ventaBindingSource.Current as Venta;

            if (venta != null)
            {
                detalleVentaBindingSource.DataSource = detVentaBL.GetAllFromDetalleVentaByVentaId(venta.Id);
            }

            try
            {
                for (int count = 0; count < ventaDataGridView.Rows.Count; count++)
                {
                    if (venta != null)
                    {
                        Cliente cli = ventaDataGridView.Rows[count].Cells[0].Value as Cliente;

                        var cliNat = (from c in cliNatBL.GetAllFromClienteNatural()
                                      where c.CliNaturalId.Equals(cli.Id)
                                      select c).SingleOrDefault();

                        var cliJuri = (from c in cliJuriBL.GetAllFromClienteJuridico()
                                       where c.IdCliJuridico.Equals(cli.Id)
                                       select c).SingleOrDefault();

                        if (cliNat != null)
                        {
                            ventaDataGridView.Rows[count].Cells[1].Value = cliNat.NombreCompleto.ToString();
                        }
                        else if (cliJuri != null)
                        {
                            ventaDataGridView.Rows[count].Cells[1].Value = cliJuri.RazonSocial.ToString();
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmRegistrarVenta frm = new FrmRegistrarVenta();
            frm.NuevaVenta();
            frm.ShowDialog();
            cargaData();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (rbtncCliente.Checked)
            {
                ventaBindingSource.DataSource = ventaBL.GetAllFromVentaByCliente(txtCriterio.Text);
                if (ventaBindingSource.Count == 0)
                {
                    MessageBox.Show("No se encuentra la Venta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (rbtnVendedor.Checked)
            {
                ventaBindingSource.DataSource = ventaBL.GetAllFromVentaByVendedor(txtCriterio.Text);
                if (ventaBindingSource.Count == 0)
                {
                    MessageBox.Show("No se encuentra la Venta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rbtncCliente.Checked)
                {
                    ventaBindingSource.DataSource = ventaBL.GetAllFromVentaByCliente(txtCriterio.Text);
                    if (ventaBindingSource.Count == 0)
                    {
                        MessageBox.Show("No se encuentra la Venta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else if (rbtnVendedor.Checked)
                {
                    ventaBindingSource.DataSource = ventaBL.GetAllFromVentaByVendedor(txtCriterio.Text);
                    if (ventaBindingSource.Count == 0)
                    {
                        MessageBox.Show("No se encuentra la Venta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var venta = ventaBindingSource.Current as Venta;
            var detVenta = detVentaBL.GetAllFromDetalleVentaByVentaId(venta.Id);

            venta.DetalleVentas = detVenta;

            if (venta != null)
            {
                FrmRegistrarVenta frm = new FrmRegistrarVenta();
                frm._venta = venta;
                frm.EditarVenta();
                frm.ShowDialog();
                cargaData();
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Venta.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ventaDataGridView_Click(object sender, EventArgs e)
        {
            var venta = ventaBindingSource.Current as Venta;

            try
            {
                for (int count = 0; count < ventaDataGridView.Rows.Count; count++)
                {
                    if (venta != null)
                    {
                        Cliente cli = ventaDataGridView.Rows[count].Cells[0].Value as Cliente;

                        var cliNat = (from c in cliNatBL.GetAllFromClienteNatural()
                                      where c.CliNaturalId.Equals(cli.Id)
                                      select c).SingleOrDefault();

                        var cliJuri = (from c in cliJuriBL.GetAllFromClienteJuridico()
                                       where c.IdCliJuridico.Equals(cli.Id)
                                       select c).SingleOrDefault();

                        if (cliNat != null)
                        {
                            ventaDataGridView.Rows[count].Cells[1].Value = cliNat.NombreCompleto.ToString();
                        }
                        else if (cliJuri != null)
                        {
                            ventaDataGridView.Rows[count].Cells[1].Value = cliJuri.RazonSocial.ToString();
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }
    }
}
